package cn.jhc.Adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.TextView;
import cn.jhc.layout.SearchByTypeHorizontalListView;
import cn.jhc.service.SearchService;

import com.example.shilehui.R;

public class SearchListByTypeAdapter extends BaseAdapter {
	
	private Context _context;
	private ViewHolder _viewHolder;
	private List<String> _list;
	private SearchByTypeHorizontalListView _searchByTypeHorizontalListView;
	private int _position=10000000;
	
	
	public SearchListByTypeAdapter(Context context,List<String> list,SearchByTypeHorizontalListView searchByTypeHorizontalListView){
		_searchByTypeHorizontalListView=searchByTypeHorizontalListView;
		_context=context;
		_list=list;
		_searchByTypeHorizontalListView.setOnItemClickListener(new onViewItemClickListener());
		
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return _list.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		
		if(convertView==null){
			LayoutInflater inflater=(LayoutInflater)_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView=inflater.inflate(R.layout.search_gallery_item,null);
			
			_viewHolder=new ViewHolder();
			_viewHolder.textView=(TextView)convertView.findViewById(R.id.search_gallery_item_textview);
			
			convertView.setTag(_viewHolder);
			
		}else{
			
			_viewHolder=(ViewHolder)convertView.getTag();
			
		}
		if(_position!=position){
			_viewHolder.textView.setTextColor(Color.rgb(41,36,33));
		}else{
			_viewHolder.textView.setTextColor(Color.rgb(93, 166, 34));
		}
		
		if(_position==10000000&&position==0){
			_viewHolder.textView.setTextColor(Color.rgb(93, 166, 34));
			SearchService.getInstance().setType(_list.get(position));
		}

		_viewHolder.textView.setText(_list.get(position));
		_viewHolder.textView.setTag(position);	

		return convertView;
	}
	
	private class ViewHolder{
		
		TextView textView;
		
	}
	
	private class onViewItemClickListener implements OnItemClickListener{

		public onViewItemClickListener(){
			
		}
		@Override
		public void onItemClick(AdapterView<?> arg0, View view, int position,
				long id) {
			SearchService.getInstance().setType(_list.get(position));
			_position=position;
			notifyDataSetChanged();
			
			
		}
		
	}
	
	



}
